package com.fcc.wfly.printhead.controller;

import com.fcc.wfly.base.controller.RestApiController;
import com.fcc.wfly.common.domain.ErrorHandleConst;
import com.fcc.wfly.printhead.domain.PrinterStateResult;
import com.fcc.wfly.printhead.hal.PrinterStatusHistoryDetailJson;
import com.fcc.wfly.printhead.hal.PrinterStatusHistoryJson;
import com.fcc.wfly.printhead.service.PrintheadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
public class GetPrinterStatusHistoryController extends RestApiController<PrinterStatusHistoryJson> {

	private static final String API_NAME = "GetPrinterStatusHistory";
	private static final String API_PARAM_PN = "PN";
	private static final String API_PARAM_SN = "SN";
	private static final String API_PARAM_STARDATE = "startDate";
	private static final String API_PARAM_ENDDATE = "endDate";

	@Autowired
	private PrintheadService printheadService;

	@RequestMapping(API_NAME)
	public HttpEntity<PrinterStatusHistoryJson> getDeviceUtilization(HttpServletRequest request, HttpServletResponse response) {
		return process(request, response);
	}

	@Override
	protected String getApiName() {
		return API_NAME;
	}

	@Override
	protected String[] getApiParamNames() {
		return new String[] { API_PARAM_PN, API_PARAM_SN, API_PARAM_STARDATE, API_PARAM_ENDDATE };
	}

	@Override
	protected PrinterStatusHistoryJson process(Map<String, String> params) {
		String PN = params.get(API_PARAM_PN);
		String SN = params.get(API_PARAM_SN);
		String startDate = params.get(API_PARAM_STARDATE);
		String endDate = params.get(API_PARAM_ENDDATE);
		ErrorHandleConst.ParamsValidate(PN,SN,startDate,endDate);
		return process(PN, SN, startDate, endDate);
	}

	private PrinterStatusHistoryJson process(String PN, String SN, String startDate, String endDate) {
		PrinterStatusHistoryJson result = new PrinterStatusHistoryJson();
		result.setProduct_Number(PN);
		result.setSerial_Number(SN);
		result.setStart_Date(startDate);
		result.setEnd_Date(endDate);

		List<PrinterStateResult> printerStateResultList = printheadService.getPrinterStateResultAllChannelList(PN, SN, //
				startDate.replace("T", " ").replace("Z", ""), endDate.replace("T", " ").replace("Z", ""));

		List<PrinterStatusHistoryDetailJson> Status = new ArrayList<PrinterStatusHistoryDetailJson>();
		for (PrinterStateResult printerStateResult : printerStateResultList) {
			PrinterStatusHistoryDetailJson detail = new PrinterStatusHistoryDetailJson();
			detail.setChannel(printerStateResult.getChannel());
			detail.setStatus(printerStateResult.getStatus());
			detail.setStart_TS(printerStateResult.getStartDate());
			detail.setEnd_TS(printerStateResult.getEndDate());
			Status.add(detail);
		}
		result.setStatus(Status);

		return result;
	}

}